/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import businessDelegate.BusinessDelegate;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Damc
 */
public class navegacionClient extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession sesion = request.getSession();
        try {
            String peticion = request.getParameter("request");
            if(peticion==null){
                response.sendRedirect("index.jsp");
            }else{
                if(peticion.equals("editar")){
                    businessDelegate.BusinessDelegate bd = new BusinessDelegate();
                    VOs.Users user;
                    String idusuario = (String)sesion.getAttribute("username");
                    out.println(idusuario);
                    user = bd.traerUsuario(idusuario);

                    request.setAttribute("nombre", user.getName());
                    request.setAttribute("apellido", user.getLastname());
                    request.setAttribute("mail", user.getEmail());
                    request.setAttribute("tell", user.getPhone());
                    request.setAttribute("creditcard", user.getCreditcard());
                        //response.sendRedirect("crearUsuario.jsp");
                    request.getRequestDispatcher("editar.jsp").include(request, response);
                    //response.sendRedirect("editar.jsp");
                }
                if(peticion.equals("planes")){
                    businessDelegate.BusinessDelegate bd = new BusinessDelegate();
                    String idusuario = (String)sesion.getAttribute("username");
                    List<VOs.Plan> plan;
                    plan=bd.planesCliente(idusuario);
                    ArrayList<Integer> tamanos = bd.planesTamplanes(plan);
                    sesion.setAttribute("planes", plan);
                    sesion.setAttribute("tamanos", tamanos);
                    //traer los planes de bd enviando el username que esta en el http sesion
                    response.sendRedirect("planes.jsp");
                }
                if(peticion.equals("carro")){
                    System.out.println("serv navcl");
                    response.sendRedirect("carro.jsp");
                }
                if(peticion.equals("home")){
                    sesion.removeAttribute("cityList");
                    response.sendRedirect("index.jsp");
                }
                if(peticion.equals("veruno")){
                    String idplan = request.getParameter("idplan");
                    businessDelegate.BusinessDelegate bd = new BusinessDelegate();
                    try{
                        List<VOs.Reserve> res = bd.reservasCliente(idplan);
                        sesion.setAttribute("reservas", res);
                        //sesion.setAttribute("idplan", idplan);
                        request.setAttribute("idplan", idplan);
                        request.getRequestDispatcher("plan.jsp").include(request, response);
                    }catch(Exception e){
                        String mes="Se ha producido un error <br>";
                        mes=mes+e.getMessage();
                        request.setAttribute("error", mes);
                        request.getRequestDispatcher("planes.jsp").include(request, response);
                    }
                }
                if(peticion.equals("verreserva")){
                    String idplan = request.getParameter("idplan");
                    String idexterno = request.getParameter("idres");
                    businessDelegate.BusinessDelegate bd = new BusinessDelegate();
                    try{
                        //bd.
                        //sesion.setAttribute("idplan", idplan);
                        request.setAttribute("idplan", idplan);
                        request.getRequestDispatcher("plan.jsp").include(request, response);
                    }catch(Exception e){
                        String mes="Se ha producido un error <br>";
                        mes=mes+e.getMessage();
                        request.setAttribute("error", mes);
                        request.getRequestDispatcher("planes.jsp").include(request, response);
                    }

                }
            }
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet navegacionClient</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet navegacionClient at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } catch(Exception e){
            response.sendRedirect("index.jsp");
        }finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
